﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Top;
using Top.Api;
using Top.Api.Response;
using Top.Api.Request;
using System.Text;
using DBControl;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;
///使用AJAX添加购买需求，在buy.aspx页面实用.
///胡俊凯2011-07-13
namespace ProjectX.AjaxCommand
{
    /// <summary>
    /// InsertBuy 的摘要说明
    /// </summary>
    public class InsertBuy : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int cate = int.Parse(context.Request.Form["Gcid"]);
            int pcid = int.Parse(context.Request.Form["Gpcid"]);
            string cidtree = context.Request.Form["Gcidtree"];
            string title = context.Request.Form["Gtitle"];
            string owner = context.Request.Form["Gowner"];
            string pricem = context.Request.Form["Gpricem"];
            string pricex = context.Request.Form["Gpricex"];
            string datetime = context.Request.Form["Gdatetime"];
            int count = int.Parse(context.Request.Form["Gcount"]);
            string imgname = context.Request.Form["Gimgpath"];
            string sheng = context.Request.Form["Gsheng"];
            string shi = context.Request.Form["Gshi"];
            string infomation = context.Request.Form["Ginfomation"];
            string infortext = context.Request.Form["Ginfortext"];
            string name = context.Request.Form["Gname"];
            string keyprop = context.Request.Form["Gkeyprop"];
            string imgsrc = "";
            string cittextTree = context.Request.Form["GcidtextTree"];

            string isFilter = Msgfilter.Filter(infortext);

            if (!isFilter.Equals(""))
            {
                context.Response.Write(isFilter);
                context.Response.End();
                return;
            }

            if (pricex == "")
            {
                pricex = "0";
            }

            if (pricem == "")
            {
                pricem = "0";
            }

            if (imgname != "")
            {
                imgsrc = "/UploadImgs/" + imgname;
                if (File.Exists(context.Server.MapPath("/uploadTemp/" + imgname)))
                {
                    File.Move(context.Server.MapPath("/uploadTemp/" + imgname), context.Server.MapPath("/UploadImgs/" + imgname));
                }
            }

            SqlDbHelper sdh = new SqlDbHelper();
            SqlParameter p1 = new SqlParameter("@ReleaseName", context.Session["userNick"]);
            SqlParameter p2 = new SqlParameter("@Information", infomation);
            SqlParameter p3 = new SqlParameter("@ExpectpricesMax", pricex);
            SqlParameter p4 = new SqlParameter("@ExpectpricesMin", pricem);
            SqlParameter p5 = new SqlParameter("@Responder", context.Session["userNick"]);
            SqlParameter p6 = new SqlParameter("@Respondcount", 1);
            SqlParameter p7 = new SqlParameter("@Cateid", cate);
            SqlParameter p8 = new SqlParameter("@Parentcateid", pcid);
            SqlParameter p9 = new SqlParameter("@Overtime", datetime);
            SqlParameter p10 = new SqlParameter("@VolumeCount", 1);
            SqlParameter p11 = new SqlParameter("@Province", sheng);
            SqlParameter p12 = new SqlParameter("@City", shi);
            SqlParameter p13 = new SqlParameter("@Title", title);
            SqlParameter p14 = new SqlParameter("@Owner", owner);
            SqlParameter p15 = new SqlParameter("@imgsrc", imgsrc);
            SqlParameter p16 = new SqlParameter("@Count", count);
            SqlParameter p17 = new SqlParameter("@name", name);
            SqlParameter p18 = new SqlParameter("@Keyprop", keyprop);
            SqlParameter p19 = new SqlParameter("@cidtree", cidtree);
            SqlParameter p20 = new SqlParameter("@CatetextTree", cittextTree);

            SqlParameter[] paras = new SqlParameter[] { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20 };
            sdh.ExecuteNonQuery("sp_insertbuyinfo", CommandType.StoredProcedure, paras);
            context.Response.Write("ok");
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}